#include <stdio.h>
#include <stdint.h>


#define KEY_FIFO_SIZE 4 /* 按键FIFO深度  */

typedef struct
{
	uint8_t Read;					/* 缓冲区读指针1 */
	uint8_t Write;					/* 缓冲区写指针 */
    uint8_t Buf[KEY_FIFO_SIZE];		/* 键值缓冲区 */
}KEY_FIFO_T;	

KEY_FIFO_T s_tKey = {0};

void keyfifo_putin(uint8_t KeyCode) {
	s_tKey.Buf[s_tKey.Write] = KeyCode;
	if (++s_tKey.Write >= KEY_FIFO_SIZE) {
		s_tKey.Write = 0;
	}
}

uint8_t keyfifo_readout(uint8_t *KeyCode)
{
	uint8_t ret = 0;
	if (s_tKey.Read != s_tKey.Write) {
        *KeyCode = s_tKey.Buf[s_tKey.Read];
		ret = 1;
		if (++s_tKey.Read >= KEY_FIFO_SIZE) {
			s_tKey.Read = 0;
		}
	}
    return ret;
}

void main(void) {
    int i = 0;
    uint8_t key = 0;
    for(i = 0; i < 3; i++) {
        keyfifo_putin(3+i);
    }
    for(i = 0; i < 3; i++) {
        if( keyfifo_readout(&key) ) {
            printf("key is %d\r\n", key);
        }
    }
}


